/*
 * @Author: your name
 * @Date: 2021-08-31 11:24:20
 * @LastEditTime: 2021-08-31 17:36:59
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \briup\5-javascript\day07\4-迭代.js
 */

var arr = [100, 200, 300, 400];
//都 every 源数组不受影响
//某一个 有没有 some 源数组不受影响
//是否大于200
function big(value) {
    return value > 200; //等价于if(value>200) return true;else return false;
}
var r1 = arr.every(big); //false
var r2 = arr.some(big); //true
console.log(arr, r1, r2);


//重写some
Array.prototype.mySome = function(fn) {
    //var arr=this
    for (i = 0; i < this.length; i++) {
        var bool = fn(this[i], i, this);
        if (bool) return true;
    }
    return false;
};



//filter 过滤  返回的是满足要求的数据项组成的数组
var r3 = arr.filter(big);
console.log(r3); //[300,400]
//重写filter 重构
Array.prototype.myFilter = function(fun) {
    var result = []
    for (var index in this) {
        var bool = fun(this[index], index, this);
        if (bool) {
            return result.push(this[index]);
        }
    }
    return result;
}

//map 一一映射 返回新的数组
var r4 = arr.map(big);
console.log(r4); //[ false, false, true, true ]
//重写map
Array.prototype.myMap = function(foo) {
    this.forEach(function(v, k, arr) {

    })
}

//forEach 没有返回值，所以作为循环遍历
arr.forEach(function(value, index) {
    console.log(index, value);
});